package cn.virens.service.impl.platform.wxmp;

import java.util.List;

import org.springframework.stereotype.Service;

import cn.virens.components.page.PageParamUser;
import cn.virens.database.common.BaseServiceImpl;
import cn.virens.database.common.mapper.ExampleJoin;
import cn.virens.database.common.mapper.ExampleWrapper;
import cn.virens.database.mysql.mapper.wxmp.WxmpMenusMapper;
import cn.virens.database.mysql.model.wxmp.WxmpMenus;
import cn.virens.service.inter.platform.wxmp.WxmpMenusService;
import cn.virens.utils.PermissionUtil;

/**
 * 菜单管理 - wxmp_menus
 *
 * @author Virens
 * @since  2019-11-09
 */
@Service
public class WxmpMenusServiceImpl extends BaseServiceImpl<WxmpMenusMapper, WxmpMenus> implements WxmpMenusService {

	public WxmpMenusServiceImpl() {
		super(WxmpMenus.class);
	}

	@Override
	protected List<?> doPageQuery(PageParamUser param) {
		ExampleWrapper example = ExampleWrapper.of(clazz);
		example.selectProperties("wxmp_menus.id", "wxmp_menus.uuid", "wxmp_menus.title", "wxmp_menus.appid", "wxmp_menus.menuid", "wxmp_base.`name` as wxname");
		example.selectProperties("wxmp_menus.create_time", "wxmp_menus.update_time", "wxmp_base.res_group", "sys_user_group.`name` AS res_groupn");

		example.addJoinTable(ExampleJoin.inner("wxmp_base", "wxmp_menus.appid = wxmp_base.appid"));
		example.addJoinTable(ExampleJoin.left("sys_user_group", "sys_user_group.identity = wxmp_base.res_group"));

		if (param != null && !PermissionUtil.isCanViewAllData(param)) {
			example.and().andCondition(PermissionUtil.str(param, "wxmp_base."));
		}

		example.and((criteria) -> {
			criteria.andCondition("wxmp_base.`enable` = 1");
			criteria.andCondition("wxmp_menus.appid = ", criteriaEqual(param, "_appid"));
			criteria.andCondition("wxmp_menus.appid LIKE ", criteriaLike(param, "appid"));
			criteria.andCondition("wxmp_menus.title LIKE ", criteriaLike(param, "title"));
		});

		return mBaseMapper.selectJoinMap(example);
	}

	@Override
	public int deleteByUuid(String uuid) {
		ExampleWrapper example = ExampleWrapper.of(clazz);
		example.and().andEqualTo("uuid", uuid);

		return mBaseMapper.deleteByExample(example);
	}

	@Override
	public WxmpMenus selectByUuid(String uuid) throws Exception {
		ExampleWrapper example = ExampleWrapper.of(clazz);
		example.and().andEqualTo("uuid", uuid);

		return mBaseMapper.selectOne(example);
	}
}
